package com.tgy.leetcode;

/**
 * @Author: tgy
 * @Date: 1/9/21 9:48 AM
 * <p>
 * https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/
 */
public class _122_买卖股票的最佳时机_II_02 {

    public int maxProfit(int[] prices) {

        if (prices == null || prices.length <= 1) {

            return 0;
        }

        return find(prices, 0, 2);
    }

    /**
     * @param prices 股票每天的价格
     * @param index  当前天数
     * @param flag   前一天的状态 1代表买，2代表卖
     * @return
     */
    private int find(int[] prices, int index, int flag) {

//        int dp[index][flag]
        if (index == prices.length) {

            return 0;
        }


        // 不做任何操作，直接去下一天
        int nothingProfit = find(prices, index + 1, flag);

        int dothingProfit = 0;
        if (flag == 1) {
            // 前一天是买，当前只能卖
            dothingProfit = find(prices, index + 1, 2) + prices[index];
        } else {
            // 前一天是卖，当前只能买
            dothingProfit = find(prices, index + 1, 1) - prices[index];
        }

        return Math.max(nothingProfit, dothingProfit);
    }

    public static void main(String[] args) {

        _122_买卖股票的最佳时机_II_02 shares = new _122_买卖股票的最佳时机_II_02();

        int maxProfit = shares.maxProfit(new int[]{1,2,3,4,5,6});

        System.out.println(maxProfit);


    }
}
